import axios, {Method} from "axios";
import {ElMessage} from 'element-plus'
import router from "@/router";
const baseUrl="/letter-api"
const http = axios.create({
    baseURL: baseUrl,
    timeout: 5000,
})
http.interceptors.request.use((request) => {
    let token = window.localStorage.getItem("token")
    request.headers = {
        'jctoken': token ? token : ''
    }
    return request
})
http.interceptors.response.use((res) => {
    if (res.data.code == 500) {
        ElMessage.warning({message: res.data.msg})
    }
    if (res.data.code === 401 || res.data.code === 403) {
        router.push("/login")
    } else {
    }
    return res.data.data
})
const requestWithToken = (url: string, method: Method, data: any): Promise<any> => {
    if (method == 'get' || method == 'GET') {
        return http({url, method, params: data})
    } else {
        return http({url, method, data: data})
    }
}
export default requestWithToken